package com.qlm.tools;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.qlm.poi.Data;


public class ListUtil {
 
    public static List<Data> intervalMerger(List<Data> intervalList, Data i,boolean add){
        if(intervalList.size()==0||add){
            intervalList.add(i);
        }
         
        Comparator<Data> intervalComparator = new Comparator<Data>() {
            public int compare(Data o1, Data o2) {
                long temp = o1.getStart_code()-o2.getStart_code();
                if(temp==0){
                    return 0;
                }else if(temp>0){
                    return 1;
                }else{
                    return -1;
                }
            }
        };
        Collections.sort(intervalList, intervalComparator);
        return intervalList;
    }
    
    /**
     * 判断data是否存在intervalList
     * */
    public static boolean whetherOverlap(List<Data> intervalList, Data i) {
        if(intervalList.size()==0)
                return false;
        intervalList = intervalMerger(intervalList,i,false);
        for (int j = 0; j < intervalList.size(); j++) {
    	   Data interval = (Data) intervalList.get(j);
    	   //判断当前的date是否存在list
    	   if (interval.getDate().equals(i.getDate())) {
    		   if ((interval.getStart_code() <= i.getStart_code() && interval.getEnd_code() >= i.getEnd_code())||
    				   (interval.getStart_code() >= i.getStart_code() && interval.getStart_code() <= i.getEnd_code())||
    				   (interval.getEnd_code() >= i.getStart_code() && interval.getEnd_code() <= i.getEnd_code())||
    				   (interval.getStart_code() >= i.getStart_code() && interval.getEnd_code() <= i.getEnd_code()))
    			   return true;
    	   }
        }
           return false;
    }
    /**
     * 判断data是否存在intervalList
     * */
    public static boolean whetherOverlap1(List<Data> intervalList, Data i) {
    	intervalList.remove(i);
        if(intervalList.size()==0)
                return false;
        intervalList = intervalMerger(intervalList,i,false);
        for (int j = 0; j < intervalList.size(); j++) {
    	   Data interval = (Data) intervalList.get(j);
    	   //判断当前的date是否存在list
    	   if (interval.getDate().equals(i.getDate())) {
    		   if ((interval.getStart_code() <= i.getStart_code() && interval.getEnd_code() >= i.getEnd_code())||
    				   (interval.getStart_code() >= i.getStart_code() && interval.getStart_code() <= i.getEnd_code())||
    				   (interval.getEnd_code() >= i.getStart_code() && interval.getEnd_code() <= i.getEnd_code())||
    				   (interval.getStart_code() >= i.getStart_code() && interval.getEnd_code() <= i.getEnd_code()))
    			   return true;
    	   }
        }
           return false;
    }
}
